package jgl.vajra.management.part.mine.pop;

import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;

import java.util.ArrayList;
import java.util.List;

import jgl.vajra.management.R;
import jgl.vajra.management.databinding.PopAllPeriodsBinding;
import jgl.vajra.management.widget.utils.UIUtils;

public class AllPeriodsPop {

    private FragmentActivity activity;
    private View mPopRootView;
    private PopupWindow menuPop;
    private AllPeriodsPop.AllPeriodsListener listener;
    private String year = "2022";
    private String month = "4";

    public AllPeriodsPop(FragmentActivity activity) {

        this.activity = activity;
    }


    private void initPopupWindow() {
        mPopRootView = View.inflate(UIUtils.getContext(), R.layout.pop_all_periods, null);
        PopAllPeriodsBinding mBinding = DataBindingUtil.bind(mPopRootView);

        menuPop = new PopupWindow(mPopRootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        menuPop.setOutsideTouchable(false);
        menuPop.setBackgroundDrawable(new BitmapDrawable());
        menuPop.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
        menuPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //popupwindow消失的时候恢复成原来的透明度
                backgroundAlpha(1f);
            }
        });
        mBinding.setView(this);

        mBinding.options1.setCyclic(false);
        List<String> mOptionsItem1 = new ArrayList<>();
        for (int i = 2010; i < 2030; i++) {
            mOptionsItem1.add(i + "");
        }

        mBinding.options1.setAdapter(new ArrayWheelAdapter(mOptionsItem1));
        mBinding.options1.setOnItemSelectedListener(index -> year = mOptionsItem1.get(index));

        mBinding.options1.setLineSpacingMultiplier(2f);//行间距
        mBinding.options1.setTextColorCenter(R.color.color_0f2cbb);
        mBinding.options1.setCurrentItem(12);

        mBinding.options2.setCyclic(false);
        List<String> mOptionsItem2 = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            mOptionsItem2.add(i + "");
        }

        mBinding.options2.setAdapter(new ArrayWheelAdapter(mOptionsItem2));
        mBinding.options2.setOnItemSelectedListener(index -> month = mOptionsItem2.get(index));
        mBinding.options2.setLineSpacingMultiplier(2f);//行间距
        mBinding.options2.setTextColorCenter(R.color.color_0f2cbb);
        mBinding.options2.setCurrentItem(3);
    }

    public void showPopupWindow(View v) {
        if (menuPop == null)
            initPopupWindow();
        int[] location = new int[2];
        v.getLocationOnScreen(location);  //获取控件的位置坐标
        //获取自身的长宽高
        mPopRootView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        backgroundAlpha(0.5f);

//        menuPop.showAtLocation(v, Gravity.NO_GRAVITY, (int) (location[0] - DisplayUtils.getDimension(R.dimen.dp_4)), (int) (location[1] - mPopRootView.getMeasuredHeight() - DisplayUtils.getDimension(R.dimen.dp_4)));  //显示指定控件的上方
        menuPop.showAsDropDown(v, 0, 0);    //显示指定控件的下方
    }

    public boolean isShowing() {
        return menuPop != null && menuPop.isShowing();
    }

    public void dismiss() {
        if (menuPop != null && menuPop.isShowing())
            menuPop.dismiss();
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        if (activity == null) return;
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        activity.getWindow().setAttributes(lp);

    }

    public void setAllPeriodsListener(AllPeriodsPop.AllPeriodsListener listener) {
        this.listener = listener;

    }

    public interface AllPeriodsListener {
        void onDetermine(String year, String month);
    }

    public void onAllTime() {
        if (listener != null)
            listener.onDetermine("", "");
        dismiss();
    }

    public void onDetermine() {
        if (listener != null)
            listener.onDetermine(year, month);
        dismiss();
    }
}


